package com.yida.gwj.config.controller;

import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.domain.BaseEntity;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.api.domain.GwjConfFlow;
import com.yida.gwj.config.domain.dto.GwjConfFlowDTO;
import com.yida.gwj.config.domain.request.GwjConfFlowRequest;
import com.yida.gwj.config.service.IGwjConfFlowService;
import com.yida.gwj.config.service.IGwjConfFlowStepService;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务流程Controller
 *
 * 修订20230112 hmh(修改地址，方法名替换，重复对象构建，提示信息修改)
 *
 * @author lqq
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/flow")
@Api(tags = "业务流程")
public class GwjConfFlowController extends BaseController {

	@Autowired
	private IGwjConfFlowService gcFlowService;

	@Autowired
	private IGwjConfFlowStepService gcFlowStepService;

	/**
	 * 查询业务流程列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("gwj:config:flow:list")
	public TableDataInfo list(GwjConfFlow gcFlow) {
		//只查未删除的
		gcFlow.setDelFlag(UserStatus.OK.getCode());
		startPage();
		List<GwjConfFlow> list = gcFlowService.selectGwjConfFlowList(gcFlow);
		return GwjConvertUtil.convertTable(getDataTable(list), GwjConfFlowDTO.class);
	}

	/**
	 * 查询业务流程列表
	 */
	@GetMapping("/list/allValid")
	public AjaxResult listAllValid() {
		List<GwjConfFlow> list = gcFlowService.selectGwjConfFlowByStatus(UserStatus.OK.getCode());
		if (StringUtils.isEmpty(list)) {
			return AjaxResult.success(new ArrayList<>());
		}
		return AjaxResult.success(GwjConvertUtil.convertList(list, GwjConfFlowDTO.class));
	}

	/**
	 * 导出业务流程列表
	 */
	@PostMapping("/export")
	@RequiresPermissions("gwj:config:flow:export")
	@Log(title = "业务流程", businessType = BusinessType.EXPORT, systemName = "gwj")
	public void export(HttpServletResponse response, GwjConfFlow gcFlow) {
		List<GwjConfFlow> list = gcFlowService.selectGwjConfFlowList(gcFlow);
		ExcelUtil<GwjConfFlow> util = new ExcelUtil<>(GwjConfFlow.class);
		util.exportExcel(response, list, "业务流程数据");
	}

	/**
	 * 获取业务流程详细信息
	 */
	@GetMapping(value = "/{flowId}")
	@RequiresPermissions("gwj:config:flow:query")
	public AjaxResult getInfo(@PathVariable("flowId") Long flowId) {
		return AjaxResult.success(GwjConvertUtil.convert(gcFlowService.selectGwjConfFlowByFlowId(flowId), GwjConfFlowDTO.class));
	}

	/**
	 * 获取业务流程步骤详细信息
	 */
	@GetMapping(value = "/step/{flowId}")
	@RequiresPermissions("gwj:config:flow:query")
	public AjaxResult getStepInfo(@PathVariable("flowId") Long flowId) {
		return AjaxResult.success(gcFlowStepService.selectGwjConfFlowStepListByFlowId(flowId));
	}

	/**
	 * 新增业务流程
	 */
	@PostMapping
	@RequiresPermissions("gwj:config:flow:add")
	@Log(title = "业务流程", businessType = BusinessType.INSERT, systemName = "gwj")
	public AjaxResult add(@Validated(BaseEntity.Save.class) @RequestBody GwjConfFlow gcFlow) {
		return toAjax(gcFlowService.insertGwjConfFlow(gcFlow));
	}

	/**
	 * 流程绑定步骤
	 */
	@PostMapping("/addStep")
	@RequiresPermissions("gwj:config:flow:addStep")
	@Log(title = "业务流程-绑定步骤", systemName = "gwj")
	public AjaxResult addFlowStep(@Validated @RequestBody GwjConfFlowRequest request) {
		return toAjax(gcFlowStepService.batchGwjConfFlowStep(request.getFlowId(), request.getGcflowSteps()));
	}

	/**
	 * 修改业务流程
	 */
	@PutMapping
	@RequiresPermissions("gwj:config:flow:edit")
	@Log(title = "业务流程", businessType = BusinessType.UPDATE, systemName = "gwj")
	public AjaxResult edit(@Validated(BaseEntity.Update.class) @RequestBody GwjConfFlow gcFlow) {
		return toAjax(gcFlowService.updateGwjConfFlow(gcFlow));
	}

	/**
	 * 删除业务流程
	 */
	@DeleteMapping("/{flowIds}")
	@RequiresPermissions("gwj:config:flow:remove")
	@Log(title = "业务流程", businessType = BusinessType.DELETE, systemName = "gwj")
	public AjaxResult remove(@PathVariable Long[] flowIds) {
		return toAjax(gcFlowService.deleteGwjConfFlowByFlowIds(flowIds));
	}


}
